package cn.lyh.edu.controller;

import cn.lyh.annotation.EnableSysLog;
import cn.lyh.base.BaseResult;
import cn.lyh.exception.BadRequestException;
import cn.lyh.edu.domain.EduLesson;
import cn.lyh.edu.service.IEduLessonService;
import cn.lyh.edu.service.dto.LessonQueryCriteria;
import cn.lyh.utils.PageVo;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

/**功能描述：课程信息前端控制器
 */
@RestController
@RequestMapping("lesson")
public class EduLessonController {

    private final IEduLessonService eduLessonService;

    public EduLessonController(IEduLessonService eduLessonService) {
        this.eduLessonService = eduLessonService;
    }

//    @EnableSysLog("【后台】获取课程列表数据")
    @GetMapping
    public ResponseEntity<Object> getList(LessonQueryCriteria queryCriteria, PageVo pageVo){

        Pageable pageable = PageRequest.of(pageVo.getPageIndex()-1, pageVo.getPageSize(),
                Sort.Direction.DESC,"id");
        return new ResponseEntity<>(eduLessonService.getList(queryCriteria,pageable), HttpStatus.OK);

    }

//    @EnableSysLog("【后台】添加课程信息")
    @PostMapping
    public BaseResult addLesson(@RequestBody EduLesson eduLesson){
        eduLessonService.addLesson(eduLesson);
        return BaseResult.success("添加成功");
    }

//    @EnableSysLog("【后台】根据ID获取课程详情信息")
    @GetMapping("/{id}")
    public BaseResult detail(@PathVariable Long id){
        if (null==id){
            throw new BadRequestException("获取信息失败！");
        }else {
            EduLesson dbEduLesson = eduLessonService.getById(id);
            return BaseResult.success(dbEduLesson);
        }

    }

//    ("【后台】更新课程信息")
    @PutMapping
    public BaseResult editLesson(@RequestBody EduLesson eduLesson){
        eduLessonService.editLesson(eduLesson);
        return BaseResult.success("更新成功");
    }

//    @EnableSysLog("【后台】删除课程信息")
    @DeleteMapping("/{id}")
    public BaseResult delete(@PathVariable Long id){
        if (null==id){
            throw new BadRequestException("删除信息失败");
        }else {
            eduLessonService.deleteById(id);
            return BaseResult.success("删除成功");
        }
    }

}
